package it.truestoryfactory.tsq.services.util;

import it.truestoryfactory.tsq.services.msg.TSMHandler;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import android.util.Log;


public class SkeletonThread extends Thread {
	Socket socket=null;
	InputStream is =null;
	MessengerInterface skeleton=null;
	
	public SkeletonThread(Socket socket, SkeletonSingleton skeleton) {
		this.socket=socket;
		try {
			this.is=socket.getInputStream();
			Log.d(this.getClass().getName(), "skeleton thread got a imput stream");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.skeleton=skeleton;
	}
	
	protected void finalize() throws Throwable {
		this.is.close();
		this.socket.close();
		super.finalize();
	}
	
	public void run() {
		
		try {
			TSMHandler handler = new TSMHandler();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(false);
			SAXParser parser = factory.newSAXParser();			
			parser.parse(is, handler);
			Log.d(this.getClass().getName(), "skeleton parsed the imput stream");
			skeleton.receiveMessage(handler.getMessage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}

}
